package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.InStockConverter;
import com.goods.business.mapper.InStockInfoMapper;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.model.system.User;
import com.goods.common.response.ActiveUser;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.*;
import com.goods.common.vo.system.PageVO;
import com.goods.common.vo.system.UserInfoVO;
import lombok.val;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.awt.util.IdentityArrayList;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.net.CookieHandler;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class InStockServiceImpl implements InStockService {
    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private InStockInfoMapper inStockInfoMapper;
    @Autowired
    private ProductMapper productMapper;

    //分页查询入库记录
    @Override
    public PageVO<InStockVO> findInStockList(Integer pageNum, Integer pageSize, InStockVO inStockVO) {
        PageHelper.startPage(pageNum,pageSize);
        //InStockConverter inStockConverter = new InStockConverter();
        Example example = new Example(InStock.class);
        Integer type = inStockVO.getType();
        String inNum = inStockVO.getInNum();
        Integer status = inStockVO.getStatus();
        Date createTime = inStockVO.getCreateTime();
        Date modified = inStockVO.getModified();
        Date startTime = inStockVO.getStartTime();
        Date endTime = inStockVO.getEndTime();
        Example.Criteria criteria = example.createCriteria();
        if (type != null && !"".equals(type)){
            criteria.andEqualTo("type",type);
        }
        if (inNum != null && !"".equals(inNum)){
            criteria.andEqualTo("inNum",inNum);
        }
        if (status != null && !"".equals(status)){
            criteria.andEqualTo("status",status);
        }
        /*if (startTime != null && endTime !=null){
            criteria.andBetween("endTime",startTime,endTime);
        }*/
        List<InStock> inStocks = inStockMapper.selectByExample(example);
        List<InStockVO> inStockVOList = new ArrayList<>();
        for (InStock inStock : inStocks) {
            Supplier supplier = supplierMapper.selectByPrimaryKey(inStock.getSupplierId());
            InStockVO inStock1 = new InStockVO();
            inStock1.setPhone(supplier.getPhone());
            inStock1.setSupplierName(supplier.getName());
            BeanUtils.copyProperties(inStock,inStock1);
            inStockVOList.add(inStock1);
        }
        //List<InStockVO> inStockVOS = inStockConverter.converterToInStockVOList(inStocks);
        PageInfo<InStock> info = new PageInfo<>(inStocks);
        return new PageVO<>(info.getTotal(),inStockVOList);
    }
    //移入回收站
    @Override
    public Object remove(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setModified(new Date());
        inStock.setStatus(1);
        return inStockMapper.updateByPrimaryKey(inStock);
    }
    //回收站放回
    @Override
    public Object back(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setModified(new Date());
        inStock.setStatus(0);
        return inStockMapper.updateByPrimaryKey(inStock);
    }
    //回收站删除
    @Override
    public Object delete(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        return inStockMapper.deleteByPrimaryKey(inStock);
    }
    //详情明细
    @Override
    public InStockDetailVO detail(Long id, Integer pageNum, Integer pageSize,InStockDetailVO inStockDetailVO) {
       /* //PageHelper.startPage(pageNum,pageSize);
        InStock inStock = inStockMapper.selectByPrimaryKey(id);//查instock表

        Long inStockId = inStock.getSupplierId();
        Supplier supplier = supplierMapper.selectByPrimaryKey(inStockId);//根据supplierid查supplier表

        Example example=new Example(InStockInfo.class);
        String inNum = inStock.getInNum();//获取instock的innum
        example.createCriteria().andEqualTo("inNum",inNum);
        List<InStockInfo> inStockInfos = inStockInfoMapper.selectByExample(example);//根据instock的innum获取instockinfo表

        //List<InStockItemVO> list = new ArrayList<>();
        Example example1 = new Example(Product.class);
        for (InStockInfo inStockInfo : inStockInfos) {
            String pNum = inStockInfo.getPNum();//获取instockinfo表的pnum
            example1.createCriteria().andEqualTo("pNum",pNum);
            List<Product> products = productMapper.selectByExample(example1);//根据instockinfo表的pnum获取product表
            List<InStockItemVO> collect = products.stream().map(product -> {
                InStockItemVO inStockItemVO = new InStockItemVO();
                BeanUtils.copyProperties(product, inStockItemVO);
                return inStockItemVO;
            }).collect(Collectors.toList());

            inStockDetailVO.setItemVOS(collect);
        }
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);

        inStockDetailVO.setStatus(inStock.getStatus());
        inStockDetailVO.setSupplierVO(supplierVO);

        return inStockDetailVO;*/
        //获取入库表
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(inStock,inStockDetailVO);
        //根据入库表查询物资来源
        Supplier supplier = supplierMapper.selectByPrimaryKey(inStock.getSupplierId());
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        inStockDetailVO.setSupplierVO(supplierVO);

        String inNum = inStock.getInNum();//入库单号
        //查询该单所有的物资
        Example o = new Example(InStockInfo.class);
        o.createCriteria().andEqualTo("inNum",inNum);
        List<InStockInfo> inStockInfoList = inStockInfoMapper.selectByExample(o);
        inStockDetailVO.setTotal(new PageInfo<>(inStockInfoList).getTotal());
        for (InStockInfo inStockInfo : inStockInfoList) {
            String pNum = inStockInfo.getPNum();
            //查出物资
            Example o1 = new Example(Product.class);
            o1.createCriteria().andEqualTo("pNum",pNum);
            List<Product> products = productMapper.selectByExample(o1);
            Product product = products.get(0);

            InStockItemVO inStockItemVO = new InStockItemVO();
            BeanUtils.copyProperties(product,inStockItemVO);
            inStockItemVO.setCount(inStockInfo.getProductNumber());
            inStockDetailVO.getItemVOS().add(inStockItemVO);

        }
        return inStockDetailVO;
    }
    //物资入库
    @Override
    public Object addIntoStock(InStockVO inStockVO) {

        InStock inStock = new InStock();
        BeanUtils.copyProperties(inStockVO,inStock);
        inStock.setCreateTime(new Date());
        inStock.setModified(new Date());
        inStock.setStatus(2);
        inStock.setInNum(UUID.randomUUID().toString());

        ActiveUser principal = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        inStock.setOperator(principal.getUser().getUsername());

        //1先判断SupplierVO有无id
        //2无id先保存Supplier表 获取id @GeneratedValue(generator = "JDBC")
        if (inStockVO.getSupplierId() == null) {
            Supplier supplier = new Supplier();
            BeanUtils.copyProperties(inStockVO,supplier);
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);
            inStock.setSupplierId(supplier.getId());
        } else {
            //有id直接保存
            inStock.setSupplierId(inStockVO.getSupplierId());
        }

        inStock.setProductNumber(inStockVO.getProducts().size());
        inStock.setSupplierId(inStock.getSupplierId());
        return inStockMapper.insert(inStock);
    }

    //审批通过
    @Override
    public Object publish(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setModified(new Date());
        inStock.setStatus(0);
        return inStockMapper.updateByPrimaryKey(inStock);
    }



}
